package androidx.blogger.tasks;

import com.google.gwt.user.client.rpc.AsyncCallback;

import androidx.blogger.BloggerApplication;
import androidx.gdata.blogger.BlogCollection;
import androidx.util.GUITask;
import androidx.util.GUITaskQueue;
import androidx.util.ProgressIndicator;

public class GetBlogCollectionTask implements GUITask {
	private AsyncCallback<BlogCollection> callback;
	private BlogCollection feed;
	
	public GetBlogCollectionTask(AsyncCallback<BlogCollection> callback) {
		this.callback = callback;
	}
	
	public static void execute(AsyncCallback<BlogCollection> callback) {
		execute(null, callback);
	}
	
	public static void execute(ProgressIndicator progressIndicator, AsyncCallback<BlogCollection> callback) {
		GUITaskQueue.getInstance().addTask(progressIndicator, new GetBlogCollectionTask(callback));
	}
	/**
	 * This gets executed on a background thread
	 */
	public void executeNonGuiTask() throws Exception {
		feed = BloggerApplication.getInstance().getServer().getBlogCollection();
	}
	
	/**
	 * This gets executed on the main gui thread AFTER executeNonGuiTask, but only
	 * if executeNonGuiTask did NOT throw an exception
	 */
	public void after_execute() {
		callback.onSuccess(feed);
	}


	/**
	 * This gets executed on the main gui thread AFTER executeNonGuiTask if it threw an
	 * exception.
	 */
	public void onFailure(Throwable t) {
		callback.onFailure(t);
	}

}
